/**
  ******************************************************************************
  * @file    main.c
  * @author  MCU Application Team
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
  * All rights reserved.</center></h2>
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "APDS9960.h" 
//#include "AP3216c.h"
#include "User_UART1.h"
#include "software_i2c.h"
#include "SEGGER_RTT.h"
#include "STK36C61.h"
#include "PL2219.h"
#include "PL2219C.h"

#ifdef __cplusplus
extern "C" {
#endif


/* Private define ------------------------------------------------------------*/
#define DATA_LENGTH      15                 /* Data length*/
#define I2C_ADDRESS      0x48               /* Local address 0xA0 */
#define I2C_SPEEDCLOCK   100000             /* Communication speed 100K */
#define I2C_DUTYCYCLE    I2C_DUTYCYCLE_16_9 /* Duty cycle */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef I2cHandle;
uint8_t aTxBuffer[15] = {1, 2};
uint8_t aRxBuffer[15] = {0, 0};


/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void APP_SystemClockConfig(void);

/**
  * @brief  Application Entry Function.
  * @retval int
  */
int main(void)
{
  /* Reset of all peripherals, Initializes the Systick. */
  HAL_Init();
  
  /* System clock configuration */
  APP_SystemClockConfig();
	
	HAL_Delay(1000);
	
	
	printf("hello world\r\n");
	
//	#if LOG_ENABLE
  User_USART1_Init();//���ڳ�ʼ��

//SEGGER_RTT_Init();
//	#endif
//	__HAL_RCC_GPIOA_CLK_ENABLE();                          /* Enable GPIOA clock */
//	//__HAL_RCC_GPIOB_CLK_ENABLE();                          /* Enable GPIOB clock */
//	GPIO_InitTypeDef  gpio_init;
//  gpio_init.Pin = GPIO_PIN_1;
//	gpio_init.Mode = GPIO_MODE_OUTPUT_OD;            /* Push-pull output */
//  gpio_init.Pull = GPIO_PULLUP;                    /* Enable pull-up */
//  gpio_init.Speed = GPIO_SPEED_FREQ_VERY_HIGH;          /* GPIO speed */  
//	HAL_GPIO_Init(GPIOA, &gpio_init);
//	
//	while(1){
//			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
//		  HAL_Delay(100);
//			HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
//		  HAL_Delay(100);
//	}
//	
//  __HAL_RCC_GPIOB_CLK_ENABLE();                          /* Enable GPIOA clock */
//  HAL_GPIO_DeInit(GPIOB,GPIO_PIN_3|GPIO_PIN_4);
//  GPIO_InitTypeDef  gpio_init;
//  gpio_init.Pin = GPIO_PIN_3|GPIO_PIN_4;
//  gpio_init.Mode = GPIO_MODE_OUTPUT_PP;            /* Push-pull output */
//  gpio_init.Pull = GPIO_NOPULL;                    /* Enable pull-up */
//  gpio_init.Speed = GPIO_SPEED_FREQ_LOW;          /* GPIO speed */ 
//  HAL_GPIO_Init(GPIOB, &gpio_init);
//  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3 | GPIO_PIN_4, GPIO_PIN_SET);
//HAL_Delay(100);
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3 | GPIO_PIN_4, GPIO_PIN_RESET);
//HAL_Delay(100);
/* I2C initialization */
	

//  I2cHandle.Instance             = I2C;                       /* I2C */
//  I2cHandle.Init.ClockSpeed      = I2C_SPEEDCLOCK;            /* I2C communication speed */
//  I2cHandle.Init.DutyCycle       = I2C_DUTYCYCLE;             /* I2C duty cycle */
//  I2cHandle.Init.OwnAddress1     = I2C_ADDRESS;               /* I2C address */
//  I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;   /* Prohibit broadcast calls */
//  I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;     /* Allow clock extension */
//  if (HAL_I2C_Init(&I2cHandle) != HAL_OK)
//  {
//		printf("init i2c fail\r\n");
//    APP_ErrorHandler();
//  }
//	
//	
//	
//	  /*I2C master polling method sending*/
//  if (HAL_I2C_Master_Transmit(&I2cHandle, I2C_ADDRESS, (uint8_t *)aTxBuffer, 15, 5000) != HAL_OK)
//  {
//    APP_ErrorHandler();
//  }
//  /*Determine the current I2C status*/
//  while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY);
//  /*I2C host polling reception*/
//  if (HAL_I2C_Master_Receive(&I2cHandle, I2C_ADDRESS, (uint8_t *)aRxBuffer, 15, 5000) != HAL_OK)
//  {
//    APP_ErrorHandler();
//  }
//  /*Determine the current I2C status*/
//  while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY);
//  
//	while(1);
	
	
	I2C_Init();
  
	#if 0
	APDS9960_Init();
  /* infinite loop */
	APDS9960_Task();
	#else
  //AP3216C_Init();
  //AP3216C_Task();

	//STK36C61_Init();
	//STK36C61_Task();
	
	PL2219_Init();
	PL2219_Task();


 //PL2219C_Init();
 //PL2219C_Task();
	#endif
	
	while(1){
		
		  //HAL_Delay(100);
			//printf("hello\r\n");//���ڲ���
		  //SEGGER_RTT_printf(0, "Hello world!\r\n");
	}

 
//// Global Variables
//SparkFun_APDS9960 apds = SparkFun_APDS9960();
//uint8_t proximity_data = 0;


//uint16_t ambient_light = 0;
//uint16_t red_light = 0;
//uint16_t green_light = 0;
//uint16_t blue_light = 0;

//int err_code  = 0;

// // Initialize APDS-9960 (configure I2C and initial values)
//  if ( apds.init() ) {
//    printf("APDS-9960 initialization complete\r\n");
//  } else {
//    printf("Something went wrong during APDS-9960 init!\r\n");
//  }
//  
//  // Adjust the Proximity sensor gain
//  if ( !apds.setProximityGain(PGAIN_1X) ) {
//    printf("Something went wrong trying to set PGAIN\r\n");
//  }
//  
//  // Start running the APDS-9960 proximity sensor (no interrupts)
//  if ( apds.enableProximitySensor(false) ) {
//    printf("Proximity sensor is now running\r\n");
//  } else {
//    printf("Something went wrong during sensor init!\r\n");
//  }



//  // Start running the APDS-9960 light sensor (no interrupts)
//  if ( apds.enableLightSensor(false) ) {
//    printf(("Light sensor is now running\r\n"));
//  } else {
//    printf(("Something went wrong during light sensor init!\r\n"));
//  }



//    while(1){



//											// Read the proximity value
//							if ( !apds.readProximity(proximity_data) ) {
//								printf("Error reading proximity value\r\n");
//							} else {
//								printf("Proximity: %d\r\n",proximity_data);
//							}




//							// Read the light levels (ambient, red, green, blue)
//							if (  !apds.readAmbientLight(ambient_light) ||
//										!apds.readRedLight(red_light) ||
//										!apds.readGreenLight(green_light) ||
//										!apds.readBlueLight(blue_light) ) {
//								printf("Error reading light values\r\n");
//							} else {
//								printf("Ambient %d,Red %d,Green %d,Blue %d\r\n",ambient_light,red_light,green_light,blue_light);
//							}
//							
//							// Wait 250 ms before next reading
//							//vTaskDelay(250/portTICK_PERIOD_MS);





//    }




}

/**
  * @brief  System clock configuration function
  * @param  None
  * @retval None
  */
static void APP_SystemClockConfig(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /* Oscillator configuration */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; /* Select oscillator HSE, HSI, LSI, LSE */
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;                           /* Enable HSI */
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;                           /* HSI 1 frequency division */
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_24MHz;  /* Configure HSI clock 24MHz */
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS_DISABLE;               /* Close HSE bypass */
  RCC_OscInitStruct.LSIState = RCC_LSI_OFF;                          /* Close LSI */
  /*RCC_OscInitStruct.LSICalibrationValue = RCC_LSICALIBRATION_32768Hz;*/
  RCC_OscInitStruct.LSEState = RCC_LSE_OFF;                          /* Close LSE */
  /*RCC_OscInitStruct.LSEDriver = RCC_LSEDRIVE_MEDIUM;*/
  /* Configure oscillator */
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    APP_ErrorHandler();
  }

  /* Clock source configuration */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1; /* Choose to configure clock HCLK, SYSCLK, PCLK1 */
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSISYS; /* Select HSISYS as the system clock */
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;        /* AHB clock 1 division */
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;         /* APB clock 1 division */
  /* Configure clock source */
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    APP_ErrorHandler();
  }
}

/**
  * @brief  Error executing function.
  * @param  None
  * @retval None
  */
void APP_ErrorHandler(void)
{
  while (1)
  {
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* Users can add their own printing information as needed,
     for example: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
